$(function() {
    initTable();
    searchEnvAll();

    $("#envIdSearch").on("change", function () {
      envChange();
    });

    $("#clusterIdSearch").on("change", function () {
      clusterChange();
    });

    $("#appIdSearch").on("change", function () {
      appChange();
    });
});

function envChange() {
    searchAllCluster();

    fillEnv();
}

function clusterChange() {
    searchAllApp();

    fillCluster();
}

function appChange() {
    searchAllNamespace();

    fillApp();
}

function fillEnv() {
  $("#envIdAdd").val($("#envIdSearch").val());
  $("#envNameAdd").val($("#envIdSearch option:selected").text());
}

function fillCluster() {
  $("#clusterIdAdd").val($("#clusterIdSearch").val());
  $("#clusterNameAdd").val($("#clusterIdSearch option:selected").text());
}

function fillApp() {
  $("#appIdAdd").val($("#appIdSearch").val());
  $("#appNameAdd").val($("#appIdSearch option:selected").text());
}


/**
* 初始化表格
*/
function initTable() {
$('#namespaceTable').DataTable({
language: Language,
columns: [
        {
        "data": "ip",
        "title": "IP",
        "defaultContent": ""
        },
        {
        "data": "port",
        "title": "端口",
        "defaultContent": ""
        },
        {
        "data": "healthy",
        "title": "是否在线",
        "defaultContent": "",
        "render": function (data, type, row, meta) {
                if(data) {
                    return "在线";
                } else {
                    return "离线";
                }
            }
        },
        {
        "data": "weight",
        "title": "权重",
        "defaultContent": ""
        },
        {
        "data": "enabled",
        "title": "是否启用",
        "defaultContent": "",
        "render": function (data, type, row, meta) {
                if(data) {
                    return "启用";
                } else {
                    return "停用";
                }
            }
        },
        {
        "data": "createTime",
        "title": "创建时间",
        "defaultContent": ""
        },
        {
        "data": "updateTime",
        "title": "修改时间",
        "defaultContent": ""
        },
        {
        "data": "null",
        "title": "操作",
        "render": function (data, type, row, meta) {
                return `<button class="btn btn-primary" data-toggle="modal" data-target="#updateModal" onClick="modifyShow(this)">修改</button>`
            }
        }
],
dom: 'rt<"bottom"ilp><"clear">',
destroy : true,
searching: false,
processing : true,
serverSide: true,
bSort: false,
lengthMenu: [10],
autoWidth: false,


ajax: {
type: "post",
contentType: "application/json",
headers: getHeaders(),
url: searchUrl,
dataFilter: dataFilterFunc,
data: function(param) {
let json = $("#namespaceForm").serializeObject();
if (json.envId == null || json.envId == "") {
    json.envId = 0;
}
if (json.clusterId == null || json.clusterId == "") {
    json.clusterId = 0;
}
if (json.appId == null || json.appId == "") {
    json.appId = 0;
}
json.pageNum = param.start / 10 + 1;
json.pageSize = param.length;
json.orderByKey = "id"
return JSON.stringify(json)
},
error: errorFunc
}
});
}

/**
* 查询
*/
function search() {
reloadTable('namespaceTable');
}

/**
* 重置
*/
function reset() {
clearForm();

search();
}

/**
* 清空表单
*/
function clearForm() {
resetForm("namespaceForm")
}

function modifyShow(_this) {
let tr = $(_this).parent().parent();
let row = getRowDataTable('namespaceTable', tr);
row.envName = $("#envIdSearch option:selected").text();
row.clusterName = $("#clusterIdSearch option:selected").text();
row.appName = $("#appIdSearch option:selected").text();
loadForm("updateForm", row)
}

function searchEnvAll() {
	axios({
		method: 'post',
		url: searchAllEnvUrl,
		data: {}
	}).then(response => {
		let data = response.data;
		if (data.success) {
			let response = data.data;
			if(response != null) {
			    let envList = response.envs;
			    if(envList != null && envList.length > 0) {
                    for(let i = 0; i < envList.length; i++) {
                        let env = envList[i];
                        let option = {};
                        option.value = env.id;
                        option.text = env.name;
                        appendOption("envIdSearch", option);
                    }

                    envChange();
			    }
			}
		} else {
			alert(data.message);
		}
	})
}

function searchAllCluster() {
    initOptions('clusterIdSearch');
    initOptions('appIdSearch');

	axios({
		method: 'post',
		url: searchAllClusterByEnvUrl,
		data: {
		  "envId": $("#envIdSearch").val()
		}
	}).then(response => {
		let data = response.data;
		if (data.success) {
			let response = data.data;
			if(response != null) {
			    let clusterList = response.clusters;
			    if(clusterList != null && clusterList.length > 0) {
                    clearOptions('clusterIdSearch');
                    for(let i = 0; i < clusterList.length; i++) {
                        let cluster = clusterList[i];
                        let option = {};
                        option.value = cluster.id;
                        option.text = cluster.name;
                        appendOption("clusterIdSearch", option);
                    }
                    clusterChange();
			    }
			}
		} else {
			alert(data.message);
		}
	})
}

function searchAllApp() {
    initOptions('appIdSearch');

	axios({
		method: 'post',
		url: searchAllAppByClusterUrl,
		data: {
		  "clusterId": $("#clusterIdSearch").val()
		}
	}).then(response => {
		let data = response.data;
		if (data.success) {
			let response = data.data;
			if(response != null) {
			    let appList = response.apps;
			    if(appList != null && appList.length > 0) {
                    clearOptions('appIdSearch');
                    for(let i = 0; i < appList.length; i++) {
                        let app = appList[i];
                        let option = {};
                        option.value = app.id;
                        option.text = app.name;
                        appendOption("appIdSearch", option);
                    }
                    appChange();
                    search();
			    }
			}
		} else {
			alert(data.message);
		}
	})
}

function searchAllNamespace() {
    initOptions('nameSearch');

	axios({
		method: 'post',
		url: searchAllAppInstanceByAppUrl,
		data: {
		  "appId": $("#appIdSearch").val()
		}
	}).then(response => {
		let data = response.data;
		if (data.success) {
			let response = data.data;
			if(response != null) {
			    let appList = response.apps;
			    if(appList != null && appList.length > 0) {
                    for(let i = 0; i < appList.length; i++) {
                        let app = appList[i];
                        let option = {};
                        option.value = app.name;
                        option.text = app.name;
                        appendOption("nameSearch", option);
                    }
			    }
			}
		} else {
			alert(data.message);
		}
	})
}






